<?php
/**
 * @copyright: ©2024 永州微 聚商网络科技有限公司
 * @link: https://www.weijuyunKe.com
 */
namespace app\admin\model;

use app\common\model\TimeModel;
use think\Model;

class SystemRole extends TimeModel
{

    /**
     * 根据角色ID获取授权节点
     *
     * @param
     *            $authId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMenuListByAdminId($roleId)
    {
        $checkNodeList = (new SystemRoleMenu())->where('role_id', $roleId)->column('menu_id');
        $systemMenu = new SystemMenu();
        $menuList = $systemMenu->where('is_auth', 1)
            ->field('id,node,title,type,is_auth')
            ->select()
            ->toArray();
        $newNodeList = [];
        foreach ($nodelList as $vo) {
            if ($vo['type'] == 1) {
                $vo = array_merge($vo, [
                    'field' => 'node',
                    'spread' => true
                ]);
                $vo['checked'] = false;
                $vo['title'] = "{$vo['title']}【{$vo['node']}】";
                $children = [];
                foreach ($nodelList as $v) {
                    if ($v['type'] == 2 && strpos($v['node'], $vo['node'] . '/') !== false) {
                        $v = array_merge($v, [
                            'field' => 'node',
                            'spread' => true
                        ]);
                        $v['checked'] = in_array($v['id'], $checkNodeList) ? true : false;
                        $v['title'] = "{$v['title']}【{$v['node']}】";
                        $children[] = $v;
                    }
                }
                ! empty($children) && $vo['children'] = $children;
                $newNodeList[] = $vo;
            }
        }
        return $newNodeList;
    }
}